package net.alche.ext.dbUtil.connect.dbs;
import net.alche.ext.dbUtil.connect.DBConnect;
import java.sql.Connection;
import net.alche.ext.dbUtil.connect.def.DbConfig;
import java.sql.DriverManager;

//
//
// @title SqlServer连接实现类
// @author yaoliwu
// @time 2017-11-20 13:27:18
//
public class DBConnectMsSql implements DBConnect {
    String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String dbName = "";
    Connection dbConn = null;
    DbConfig config;

  public DBConnectMsSql(DbConfig config) {
    this.config = config;
    dbName = config.DB_NAME;
  }

  public Connection getConn() {
    if (dbConn == null) {
      String dbURL = "jdbc:sqlserver://" + config.DB_IP + ":" + config.DB_PORT_MSSQL + ";DatabaseName=" + dbName;
      try {
        Class.forName(driverName);
        Connection dbConn = DriverManager.getConnection(dbURL, config.DB_UID, config.DB_PWD);
        this.dbConn = dbConn;
        return dbConn;
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
    else {
      return dbConn;
    }
    return null;
  }

  public void close() {
    if (dbConn != null) {
      try {
        dbConn.close();
      }
      catch (Exception e) {}
    }
  }

  @Override
  public int getDbType() {
    return 3;
  }

  @Override
  public String getUserName() {
    return config.DB_UID;
  }

  @Override
  public String getTableRemark(String tableName) {
    return "";
  }
}